import 'package:encrypt/encrypt.dart';

class EncryptUtil {
  ///aes加密
  /// [aesKey]AesCrypt加密key
  /// [content] 需要加密的内容字符串
  static String aesEncode({required String aesKey, required String content}) {
    final key = Key.fromUtf8(aesKey);
    final iv = IV.fromLength(16); // ECB模式下，IV可能不会被使用

    final encrypter = Encrypter(AES(key, mode: AESMode.ecb, padding: 'PKCS7'));

    final encrypted = encrypter.encrypt(content, iv: iv);
    return encrypted.base64;

    // var aesEncrypter = AesCrypt(key ,'ecb', 'pkcs7');
    // final encrypter = Encrypter(AES(key, mode: AESMode.ecb, padding: 'PKCS7'));
    // return aesEncrypter.encrypt(content);
  }

  ///aes解密
  /// [aesKey]aes解密key
  /// [content] 需要加密的内容字符串
  static String aesDecode({required String aesKey, required String content}) {
    final key = Key.fromUtf8(aesKey);
    final iv = IV.fromLength(16); // ECB模式下，IV可能不会被使用

    // 假设这是您接收到的加密文本
    final encryptedText = content;

    final encrypter = Encrypter(AES(key, mode: AESMode.ecb, padding: 'PKCS7'));

    // 使用encrypter对象进行解密
    final decrypted = encrypter.decrypt(Encrypted.fromBase64(encryptedText), iv: iv);
    return decrypted;

    // var aesEncrypter = AesCrypt(key, 'ecb', 'pkcs7');
    // return aesEncrypter.decrypt(content);
  }
}
